package ee.siimkaspar.subjects.model;


import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;

@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@Entity
@Table(name = "subject_attribute")
public class SubjectAttribute implements Serializable, ModelInterface, Comparable<SubjectAttribute> {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "subject_attribute", unique = true, nullable = false)
	private Integer id;
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "subject_type_fk")
	private SubjectType subjectType;
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "subject_attribute_type_fk")
	private SubjectAttributeType subjectAttributeType;
	/**
	 * pointer to Person(subjectType.id==1) or Enterprise(subjectType.id==2)
	 */
	@Column(name = "subject_fk")
	private Integer subjectFk;
	@Column(name = "orderby")
	private Integer orderby;
	@Column(name = "value_text")
	private String valueText;
	@Column(name = "value_number", precision = 131089, scale = 0)
	private BigDecimal valueNumber;
	@Temporal(TemporalType.DATE)
	@Column(name = "value_date", length = 13)
	private Date valueDate;
	@Column(name = "data_type")
	private Integer dataType;

	public SubjectAttribute() {
	}
	
	public SubjectAttribute(SubjectAttributeType subjectAttributeType) {
		this.subjectAttributeType = subjectAttributeType;
		createNewAttributeFromType(subjectAttributeType);
	}
	
	private void createNewAttributeFromType(SubjectAttributeType subjectAttributeType) {
		this.dataType = subjectAttributeType.getDataType();
		this.orderby = subjectAttributeType.getOrderby();
		this.subjectType = subjectAttributeType.getSubjectType();
	}

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	
	public SubjectType getSubjectType() {
		return this.subjectType;
	}

	public void setSubjectType(SubjectType subjectType) {
		this.subjectType = subjectType;
	}

	public SubjectAttributeType getSubjectAttributeType() {
		return this.subjectAttributeType;
	}

	public void setSubjectAttributeType(
			SubjectAttributeType subjectAttributeType) {
		this.subjectAttributeType = subjectAttributeType;
	}

	public Integer getSubjectFk() {
		return this.subjectFk;
	}

	public void setSubjectFk(Integer subjectFk) {
		this.subjectFk = subjectFk;
	}

	public Integer getOrderby() {
		return this.orderby;
	}

	public void setOrderby(Integer orderby) {
		this.orderby = orderby;
	}

	public String getValueText() {
		return this.valueText;
	}

	public void setValueText(String valueText) {
		this.valueText = valueText;
	}

	public BigDecimal getValueNumber() {
		return this.valueNumber;
	}

	public void setValueNumber(BigDecimal valueNumber) {
		this.valueNumber = valueNumber;
	}

	public Date getValueDate() {
		return this.valueDate;
	}

	public void setValueDate(Date valueDate) {
		this.valueDate = valueDate;
	}

	public Integer getDataType() {
		return dataType;
	}

	public void setDataType(Integer dataType) {
		this.dataType = dataType;
	}

	@Override
	public String toString() {
		return "SubjectAttribute [id=" + id + ", orderby=" + orderby + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((dataType == null) ? 0 : dataType.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime
				* result
				+ ((subjectAttributeType == null) ? 0 : subjectAttributeType
						.hashCode());
		result = prime * result
				+ ((subjectFk == null) ? 0 : subjectFk.hashCode());
		result = prime * result
				+ ((subjectType == null) ? 0 : subjectType.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		SubjectAttribute other = (SubjectAttribute) obj;
		if (dataType == null) {
			if (other.dataType != null)
				return false;
		} else if (!dataType.equals(other.dataType))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (subjectAttributeType == null) {
			if (other.subjectAttributeType != null)
				return false;
		} else if (!subjectAttributeType.equals(other.subjectAttributeType))
			return false;
		if (subjectFk == null) {
			if (other.subjectFk != null)
				return false;
		} else if (!subjectFk.equals(other.subjectFk))
			return false;
		if (subjectType == null) {
			if (other.subjectType != null)
				return false;
		} else if (!subjectType.equals(other.subjectType))
			return false;
		return true;
	}

	@Override
	public int compareTo(SubjectAttribute o) {
		return orderby.compareTo(o.orderby);
	}
	@Override
	public boolean validate() {
		// TODO Auto-generated method stub
		return true;
	}
}
